﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace template_dll
{
    public partial class Form6 : Form
    {
        public Form6()
        {
            InitializeComponent();
        }

        public SqlConnection conn;
        public int actionId = 0;
        private DataTable dt = new DataTable();
        private void Form6_Load(object sender, EventArgs e)
        {
            try
            {
                if (actionId == 0) panel1.Visible = false;
                dateTimePicker1.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

                SqlStorProc.sp_params[] par = new SqlStorProc.sp_params[1];
                par[0].name = "@actionid";
                par[0].value = actionId;
                dt = new SqlStorProc().SqlProcExecWithQueryOneTable(conn, "pas_Actions_MontsPercentSel", 300, par);
                dataGridView1.DataSource = dt.DefaultView;
                dataGridView1.Columns[0].ReadOnly = true;
                dataGridView1.Columns[1].ReadOnly = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            dateTimePicker1.Value = new DateTime(dateTimePicker1.Value.Year,dateTimePicker1.Value.Month,1);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                object[] row = new object[2] { dateTimePicker1.Value.Date, textBox1.Text };
                bool fl = false;
                foreach (DataRow r in dt.Rows)
                {
                    if (Convert.ToDateTime(r[0]) == dateTimePicker1.Value)
                    {
                        r[1] = textBox1.Text;
                        fl = true;
                    }
                }
                if (!fl) dt.Rows.Add(row);
                dataGridView1.DataSource = dt.DefaultView;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (actionId > 0)
                {
                    if (MessageBox.Show("Сохранить внесенные изменения для текущей акции?", "Сохранить изменения?", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    {
                        double sum = 0;
                        foreach (DataGridViewRow row in dataGridView1.Rows)
                        {
                            if (row.Cells[1].Value != DBNull.Value) sum += Convert.ToDouble(row.Cells[1].Value);
                        }
                        DialogResult dr = System.Windows.Forms.DialogResult.Yes;
                        if (sum != 100)
                        {
                            dr = MessageBox.Show("Внимание! Итоговая сумма по всем столбцам равна " + sum + "%\nВсе равно сохранить изменения?", "Сумма не равна 100%", MessageBoxButtons.YesNo);
                        }
                        if (dr == System.Windows.Forms.DialogResult.Yes)
                        {
                            SqlStorProc.sp_params[] par = new SqlStorProc.sp_params[3];
                            par[0].name = "@actionid";
                            par[0].value = actionId;
                            par[1].name = "@month";
                            par[2].name = "@percent";

                            foreach (DataGridViewRow row in dataGridView1.Rows)
                            {
                                par[1].value = row.Cells[0].Value;
                                par[2].value = row.Cells[1].Value;
                                new SqlStorProc().SqlProcExecNonQuery(conn, "pas_Actions_MontsPercentIns", 30, par);
                            }
                            MessageBox.Show("Все данные сохранены");
                            this.DialogResult = System.Windows.Forms.DialogResult.OK;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsDigit(e.KeyChar)) && !((e.KeyChar == ',') && (textBox1.Text.IndexOf(",") == -1) && (textBox1.Text.Length != 0)))
            {
                if (e.KeyChar != (char)Keys.Back) e.Handled = true;
            }
        }
    }
}
